﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using MediaPortal.Player;
using AxUnityWebPlayerAXLib;
using MediaPortal.GUI.Library;

namespace FlashUnity
{
  public class Unity3dPlayer : IPlayer
  {
    Unity3d unity3dPlayer;
    string url;
    string playPosition;

    public override bool Play(string strFile)
    {
      url = strFile;

      unity3dPlayer = new Unity3d();
      GUIGraphicsContext.form.Controls.Add(unity3dPlayer);
      unity3dPlayer.Enabled = false;

      unity3dPlayer.LocationChanged += unity3dPlayer_LocationChanged;

      unity3dPlayer.Play(url);

      GUIPropertyManager.SetProperty("#TV.Record.percent3", 0.0f.ToString()); // set to 0, as this player doesn't support download progress reporting

      GUIWaitCursor.Init(); GUIWaitCursor.Show(); // init and show the wait cursor while buffering

      return true;
    }

    void unity3dPlayer_LocationChanged(object sender, EventArgs e)
    {
      playPosition = e.ToString();
    }

    public override void Process()
    {

          //GUIWindowManager.ActivateWindow(GUIOnlineVideoFullscreen.WINDOW_FULLSCREEN_ONLINEVIDEO);
          GUIMessage msgPb = new GUIMessage(GUIMessage.MessageType.GUI_MSG_PLAYBACK_STARTED, 0, 0, 0, 0, 0, null);
          msgPb.Label = CurrentFile;
          GUIWindowManager.SendThreadMessage(msgPb);
          SetVideoWindow();
    }

    public override void SetVideoWindow()
    {
      unity3dPlayer.Location = new Point(FullScreen ? 0 : GUIGraphicsContext.VideoWindow.X, FullScreen ? 0 : GUIGraphicsContext.VideoWindow.Y);
      unity3dPlayer.ClientSize = new Size(FullScreen ? GUIGraphicsContext.Width : GUIGraphicsContext.VideoWindow.Width, FullScreen ? GUIGraphicsContext.Height : GUIGraphicsContext.VideoWindow.Height);
      _videoRectangle = new Rectangle(unity3dPlayer.Location.X, unity3dPlayer.Location.Y, unity3dPlayer.ClientSize.Width, unity3dPlayer.ClientSize.Height);
      _sourceRectangle = _videoRectangle;
    }

    private void PlaybackEnded()
    {
      Log.Info("Unity3dPlayer:ended {0}", url);
      //playState = PlayState.Ended;
      if (unity3dPlayer != null)
      {
        unity3dPlayer.Location = new Point(0, 0);
        unity3dPlayer.ClientSize = new Size(0, 0);
        unity3dPlayer.Visible = false;
      }
    }

    public override void Pause()
    {
      //if (media != null && playState == PlayState.Playing || playState == PlayState.Paused)
      //{
      //  unity3dPlayer.Pause();
      //  if (media.State == States.Paused)
      //    playState = PlayState.Paused;
      //  else
      //    playState = PlayState.Playing;
      //}
    }

    public override void Stop()
    {
      if (unity3dPlayer != null)
      {
        //if (media.State != States.Stopped && media.State != States.Ended)
          //unity3dPlayer.Stop();
        PlaybackEnded();
      }
    }

    public override double Duration
    {
      get { return media != null ? media.Duration.TotalSeconds : 0.0; }
    }

    public override double CurrentPosition
    {
      get { return playPosition * Duration; }
    }

    public override bool Initializing
    {
      get { return playState == PlayState.Init; }
    }

    public override bool Paused
    {
      get { return playState == PlayState.Paused; }
    }

    public override bool Playing
    {
      get { return !Ended; }
    }

    public override bool Stopped
    {
      get { return Initializing || Ended; }
    }

    public override bool Ended
    {
      get { return playState == PlayState.Ended; }
    }

    public override int Speed
    {
      get { return (Convert.ToInt32(vlcCtrl.Rate)); }
      set { unity3dPlayer.Rate = value; }
    }

    public override void SeekRelative(double dTime)
    {
      double dCurTime = CurrentPosition;
      dTime = dCurTime + dTime;
      if (dTime < 0.0d) dTime = 0.0d;
      if (dTime < Duration)
      {
        SeekAbsolute(dTime);
      }
    }

    public override void SeekAbsolute(double dTime)
    {
      if (dTime < 0.0d) dTime = 0.0d;
      if (dTime < Duration)
      {
        if (unity3dPlayer == null) return;
        try
        {
          unity3dPlayer.Time = TimeSpan.FromSeconds(dTime);
        }
        catch (Exception ex) { Log.Error(ex); }
      }
    }
  }
} 
